{{ $is_list := .is_list }}
{{ $page := .page }}
{{ $link := "" }}

{{/* ***** TODO: Deprecate `url_preprint` in favour of `url_pdf` given there's now a dedicated "Preprint" pub. type. ***** */}}
{{ with $page.Params.url_preprint }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
  <a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
    {{ i18n "btn_preprint" }}
  </a>
{{ end }}
{{ $pdf := "" }}
{{ $resource := $page.Resources.GetMatch (printf "%s.pdf" $page.File.ContentBaseName) }}
{{ with $resource }}
  {{ $pdf = .RelPermalink }}
{{ else }}
  {{ if $page.Params.url_pdf }}
    {{ $pdf = $page.Params.url_pdf | relURL }}
  {{ end }}
{{ end }}

{{ with $pdf }}
<a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ . }}" target="_blank" rel="noopener">
  {{ partial "functions/get_icon" (dict "name" "document-text" "attributes" "style=\"height: 1em\" class='inline-block'") }}
  {{ i18n "btn_pdf" }}
</a>
{{ end }}
{{ $resource := $page.Resources.GetMatch "cite.bib" }}
{{ with $resource }}
<a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ .RelPermalink }}" target="_blank" data-filename="{{ .RelPermalink }}">
  {{ partial "functions/get_icon" (dict "name" "document-duplicate" "attributes" "style=\"height: 1em\" class='inline-block'") }}
  {{ i18n "btn_cite" }}
</a>
{{ end }}
{{ with $page.Params.url_code }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ partial "functions/get_icon" (dict "name" "code-bracket" "attributes" "style=\"height: 1em\" class='inline-block'") }}
  {{ i18n "btn_code" }}
</a>
{{ end }}
{{ with $page.Params.url_dataset }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ partial "functions/get_icon" (dict "name" "circle-stack" "attributes" "style=\"height: 1em\" class='inline-block'") }}
  {{ i18n "btn_dataset" }}
</a>
{{ end }}
{{ if $page.Params.projects }}
{{ range $page.Params.projects }}
  {{ with (site.GetPage (printf "project/%s" .)) }}
  <a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ .RelPermalink }}">
    {{ i18n "btn_project" }}
  </a>
  {{ else }}
    {{/* errorf "The `projects` parameter in `content/%s` references a project file, `content/project/%s`, which cannot be found. Please either set `projects = []` or fix the reference." $page.File.Path . */}}
  {{ end }}
{{ end }}
{{ else }}
{{ with $page.Params.url_project }}
<a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ . }}" target="_blank" rel="noopener">
  {{ i18n "btn_project" }}
</a>
{{ end }}
{{ end }}
{{ with $page.Params.url_poster }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ partial "functions/get_icon" (dict "name" "photo" "attributes" "style=\"height: 1em\" class='inline-block'") }}
  {{ i18n "btn_poster" }}
</a>
{{ end }}
{{ if $page.Params.slides }}
  {{ with (site.GetPage (printf "slides/%s" $page.Params.slides)) }}
  <a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ .RelPermalink }}" target="_blank">
    {{ partial "functions/get_icon" (dict "name" "presentation-chart-bar" "attributes" "style=\"height: 1em\" class='inline-block'") }}
    {{ i18n "btn_slides" }}
  </a>
  {{ end }}
{{ else }}
{{ with $page.Params.url_slides }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
  <a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
    {{ partial "functions/get_icon" (dict "name" "presentation-chart-bar" "attributes" "style=\"height: 1em\" class='inline-block'") }}
    {{ i18n "btn_slides" }}
  </a>
{{ end }}
{{ end }}
{{ with $page.Params.url_video }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ partial "functions/get_icon" (dict "name" "video-camera" "attributes" "style=\"height: 1em\" class='inline-block'") }}
  {{ i18n "btn_video" }}
</a>
{{ end }}
{{ with $page.Params.url_source }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ partial "functions/get_icon" (dict "name" "document" "attributes" "style=\"height: 1em\" class='inline-block'") }}
  {{ i18n "btn_source" }}
</a>
{{ end }}
{{ with $page.Params.doi }}
<a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="https://doi.org/{{ . }}" target="_blank" rel="noopener">
  DOI
</a>
{{ end }}
{{ range $page.Params.links }}
  {{ $pack := or .icon_pack "fas" }}
  {{ $pack_prefix := $pack }}
  {{ if in (slice "fab" "fas" "far" "fal") $pack }}
    {{ $pack_prefix = "fa" }}
  {{ end }}
  {{ $link := .url | default "" }}
  {{ $scheme := (urls.Parse $link).Scheme }}
  {{ $target := "" }}
  {{ if not $scheme }}
    {{ $resource := $page.Resources.GetMatch $link }}
    {{ if $resource }}
      {{ $link = $resource.RelPermalink }}
    {{ else }}
      {{ $link = $link | relURL }}
    {{ end }}
  {{ else if in (slice "http" "https") $scheme }}
    {{ $target = "target=\"_blank\" rel=\"noopener\"" }}
  {{ end }}
  <a class="hb-attachment-link {{ if $is_list }}hb-attachment-small{{else}}hb-attachment-large{{end}}" href="{{ $link | safeURL }}" {{ $target | safeHTMLAttr }}>
    {{ partial "functions/get_icon" (dict "name" "link" "attributes" "style=\"height: 1em\" class='inline-block'") }}
    {{ if .icon }}<i class="{{ $pack }} {{ $pack_prefix }}-{{ .icon }} {{if .name}}mr-1{{end}}"></i>{{end}}
    {{- with .name }}{{ . | safeHTML }}{{ end -}}
  </a>
{{ end }}
